﻿using System;
using System.Collections.Generic;
using System.Text;
using Sinacor.Infra.Service.Common;
using PSE.ProcessManager.Server.Process.Security;
using Sinacor.Infra.Core.ProcessManager.Server.Security;
using Sinacor.Infra.Service.Security.Services;
using Sinacor.Process.Bovespa.Distribuicao.Server.Bovespa.Cliente.Cliente;

namespace Sinacor.Process.Bovespa.Distribuicao.Server.Communication
{
    public class Cliente : BaseCommunication
    {
        #region Construtor

        public Cliente()
            : base(AuthenticationContext<SinacorAuthenticationData>.Current.AuthenticationData.LoginName, AuthenticationContext<SinacorAuthenticationData>.Current.AuthenticationData.Password, AuthenticationContext<SinacorAuthenticationData>.Current.AuthenticationData.CompanyId)
        {
        }

        #endregion

        #region Metodos

        public ProcessEntity.Cliente ObterCliente(ProcessEntity.Parameter.FiltroCliente model)
        {
            ProcessEntity.Cliente retorno = null;
            ICliente servico = null;

            try
            {

                if (retorno == null)
                {
                    using (ServiceInstance<ICliente> service = new ServiceInstance<ICliente>())
                    {
                        servico = service.TryGetInstance(this.LoginName, this.Password, this.CompanyId);

                        FiltroClienteRequest request =
                            new Translator.ProcessEntityToDataContract.Cliente().ToFiltroClienteRequest(model);

                        ClienteResponse clienteResponse = servico.ObterCliente(request);

                        retorno = new Translator.DataContractToProcessEntity.Cliente().ToCliente(clienteResponse);
                    }
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }

            return retorno;
        }

        #endregion
    }
}
